﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2009-10-30 13:06:34Z</CreationDate>
</File>
*/
#endregion

using System;

#if NETFX_CORE
using Dispatcher=Windows.UI.Core.CoreDispatcher;
using DispatcherPriority=Windows.UI.Core.CoreDispatcherPriority;
#else
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Threading;
#endif

namespace Outcoder
{
	/* TODO: [DV] Comment. */
	public static class DispatcherExtensions
	{

#if !SILVERLIGHT && !NETFX_CORE
		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
		/// <param name="priority">The DispatcherPriority for the invoke.</param>
		public static void InvokeIfRequired(this Dispatcher dispatcher,
			Action action, DispatcherPriority priority)
		{
			if (!dispatcher.CheckAccess())
			{
				dispatcher.Invoke(priority, action);
			}
			else
			{
				action();
			}
		}
#endif

		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
		public static void InvokeIfRequired(this Dispatcher dispatcher, Action action)
		{
#if NETFX_CORE
			if (!dispatcher.HasThreadAccess)
			{
#else
			if (!dispatcher.CheckAccess())
			{
#endif
			
#if SILVERLIGHT
				dispatcher.BeginInvoke(action);
#elif NETFX_CORE
#pragma warning disable 4014
				dispatcher.RunAsync(DispatcherPriority.Normal, () => action());
#pragma warning restore 4014
#else
				dispatcher.Invoke(DispatcherPriority.Normal, action);
#endif
			}
			else
			{
				action();
			}
		}

		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
//		public static void InvokeInBackgroundIfRequired(this Dispatcher dispatcher, Action action)
//		{
//			if (!dispatcher.CheckAccess())
//			{
//#if SILVERLIGHT
//				dispatcher.BeginInvoke(action);
//#else
//				dispatcher.Invoke(DispatcherPriority.Background, action);
//#endif
//			}
//			else
//			{
//				action();
//			}
//		}

		/// <summary>
		/// A simple threading extension method, to invoke a delegate
		/// on the correct thread asynchronously if it is not currently on the correct thread
		/// which can be used with DispatcherObject types.
		/// </summary>
		/// <param name="dispatcher">The Dispatcher object on which to 
		/// perform the Invoke</param>
		/// <param name="action">The delegate to run</param>
		public static void InvokeWithoutBlocking(this Dispatcher dispatcher, Action action)
		{
#if SILVERLIGHT
			dispatcher.BeginInvoke(action);
#elif NETFX_CORE
#pragma warning disable 4014
			dispatcher.RunAsync(DispatcherPriority.Low, () => action());
#pragma warning restore 4014
#else
			dispatcher.BeginInvoke(DispatcherPriority.Background, action);
#endif
		}

	}
}
